<?php
class ChallengeRunner{
	// VALEUR OK
	public $diffBetweenPosition=1;
	public $bonusAllChallenge=20;
	public $totalPointStart=500;

	public $positionNobressart;
	public $positionRossignol;
	public $positionLesFosses;
	
	public $pointsNobressart=0;
	public $pointsRossignol=0;
	public $pointsLesFosses=0;
	
	public $calculExplaination;
	
	public $nom;
	public $prenom;
	public $challengeTotal;
	
	public $info;
	
	function ChallengeRunner($nom,$prenom){
		$this->nom=$nom;
		$this->prenom=$prenom;
		
	}
	function isAllChallenge(){
		return ($this->positionNobressart>0 &&	$this->positionRossignol>0 && $this->positionLesFosses>0);
	}
	function getInfo(){
		return $this->info;
	}
	function getNom(){
		return $this->nom;
	}
	function getPrenom(){
		return $this->prenom;
	}
	function getChallengeTotal(){
		return $this->challengeTotal;
	}
	function toString(){
	 echo "nom: ".$this->nom;
	 echo "<br>";
	 echo "prenom: ".$this->prenom;	
	 echo "<br>";
	 echo "positionNobressart: ".$this->positionNobressart;
	 echo "<br>";
	 echo "positionRossignol: ".$this->positionRossignol;
	 echo "<br>";
	 echo "positionLesFosses: ".$this->positionLesFosses;
	 echo "<br>";
	
	 echo "pointsNobressart: ".$this->pointsNobressart;
	 echo "<br>";
	 echo "pointsRossignol: ".$this->pointsRossignol;
	 echo "<br>";
	 echo "pointsLesFosses: ".$this->pointsLesFosses;
	 echo "<br>";
	
	 echo "calculExplaination: ".$this->calculExplaination;
	echo "<br>";
	echo "<br>";
	 
	
	}
	function buildChallengePoints(){
		$this->setNobressartPoints();
		$this->setRossignolPoints();
		$this->setLesfossesPoints();
		$points=0;
		$bonus=0;
		if($this->isAllChallenge()){
			$points+=$this->bonusAllChallenge;
			$bonus=$this->bonusAllChallenge;
		}
		$this->info.="Bonus all challenge: ".$bonus."<br>";
		
		$max=0;
		$second=0;
		$max=$this->getBiggestScore();
		$second=$this->getSecondScore();
		
		$points+=($max);
		$points+=($second);
		$this->challengeTotal=$points;
		$this->info.="TOTAL=<br>BEST[".$max."] + SECOND BEST[".$second."] + BONUS [".$bonus."]";
		
		return $this->challengeTotal;
		
	}
	public $done=false;
	function getBiggestScore(){
		$points=$this->pointsNobressart;
		$from="NOBRESSART";
		if($points<$this->pointsLesFosses){
			$points=$this->pointsLesFosses;
			$from="LES_FOSSES";
		}
		if($points<$this->pointsRossignol){
			$points=$this->pointsRossignol;
			$from="ROSSIGNOL";
		}
		if(!$this->done){
			$this->info.="Best Score: ".$from." - Points: ".$points."<br>";	
			$this->done=true;
		}
		
		return $points;
	}
	
	function getSecondScore(){
		$points=$this->getBiggestScore();
		$work1=$points-$this->pointsLesFosses;
		$work2=$points-$this->pointsRossignol;
		$work3=$points-$this->pointsNobressart;
		if($work1==$work2 && $work1==$work3){
			return $work1;
		}
		$toKeep=0;
		$from="";
		$smallestDiff=9999999999;
		
		if($smallestDiff>$work1 && $work1!=0){
			$smallestDiff=$work1;
			$toKeep=$this->pointsLesFosses;
			$from="LES_FOSSES";
		}
		if($smallestDiff>$work2 && $work2!=0){
			$smallestDiff=$work2;
			$toKeep=$this->pointsRossignol;
			$from="ROSSIGNOL";
		}
		if($smallestDiff>$work3 && $work3!=0){
			$smallestDiff=$work3;
			$toKeep=$this->pointsNobressart;
			$from="NOBRESSART";
		}
		$this->info.="Second best Score: ".$from." - Points: ".$toKeep."<br>";
		return $toKeep;
	}
	
	function setNobressartPoints(){
		$points=$this->totalPointStart;
		$removePoints=($this->positionNobressart-1)*$this->diffBetweenPosition;
		$total=($removePoints<0?0:$points-$removePoints);
		$this->pointsNobressart= $total;
		$this->info.="Position Nobressart: ".$this->positionNobressart." - Points: ".$this->pointsNobressart."<br>";
		
	}	
	function setRossignolPoints(){
		$points=$this->totalPointStart;
		$removePoints=($this->positionRossignol-1)*$this->diffBetweenPosition;
		$total=($removePoints<0?0:$points-$removePoints);
		$this->pointsRossignol= $total;
		$this->info.="Position Rossignol: ".$this->positionRossignol." - Points: ".$this->pointsRossignol."<br>";
	}	
	function setLesfossesPoints(){
		$points=$this->totalPointStart;;
		$removePoints=($this->positionLesFosses-1)*($this->diffBetweenPosition);
		$total=($removePoints<0?0:$points-$removePoints);
		$this->pointsLesFosses= $total;
		$this->info.="Position Les Fossés: ".$this->positionLesFosses." - Points: ".$this->pointsLesFosses."<br>";
	}	
	function setNobPosition($positionNobressart){
		$this->positionNobressart=$positionNobressart;
	}
	
	function setRosPosition($positionRossignol){
		$this->positionRossignol=$positionRossignol;
	}
	
	function setLesPosition($positionLesFosses){
		$this->positionLesFosses=$positionLesFosses;
	}
	function getNobPosition(){
		return $this->positionNobressart;
	}
	
	function getRosPosition(){
		return $this->positionRossignol;
	}
	
	function getLesPosition(){
		return $this->positionLesFosses;
	}
}